package com.qjcc.service.impl;

import com.qjcc.domain.AjaxJson;
import com.qjcc.domain.Series;
import com.qjcc.domain.SeriesInclude;
import com.qjcc.domain.p.ZtTableDataEntity;
import com.qjcc.repository.p.ZtTableDataRepository;
import com.qjcc.service.ZtTableDataNewService;
import com.qjcc.util.QjztUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ZtTableDataNewServiceImpl implements ZtTableDataNewService {

    @Autowired
    private ZtTableDataRepository ztTableDataRepository;

    @Autowired
    @Qualifier("primaryJdbcTemplate")
    protected JdbcTemplate primaryJdbc;

    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");

    @Override
    public List<ZtTableDataEntity> list() {
        return ztTableDataRepository.findAll();
    }

    @Override
    public List<ZtTableDataEntity> top12() {
        return ztTableDataRepository.findTop10ByOrderByDateAsc();
    }

    @Override
    public AjaxJson whl(String queryType){
        AjaxJson j = new AjaxJson();
        Map<String, Object> m = new HashMap<>();

        List<Map<String, Object>> dateList = getDateList();
        List<Map<String, Object>> typeList = null;

        String color = "";
        if("gy".equals(queryType)){
            typeList = getTypeGyList();
            color = Series.getColor(0);

        }else if("my".equals(queryType)){
            typeList = getTypeMyList();
        }

        //横坐标
        List<String> categories = new ArrayList<>();
        for(Map<String, Object> date: dateList){
            categories.add(date.get("xDate").toString());
        }

        //纵坐标
        List<Series> series = new ArrayList<>();
        for(int i = 0; i < typeList.size(); i++){
            String typecode = (String) typeList.get(i).get("typecode");
            String typename = (String) typeList.get(i).get("typename");
            List<Double> columnData = new ArrayList<>();
            List<Float> splineData = new ArrayList<>();

            for(Map<String, Object> obj: dateList){
                Date date = (Date) obj.get("date");
                ZtTableDataEntity tableDataEntity = ztTableDataRepository.findByTypeAndDate(typecode, date);
                //维护量
                double xValue = null != tableDataEntity ? tableDataEntity.getWhl() / 1000 : 0;
                //增长率
                float zzl = QjztUtil.getResultFor2Point(null != tableDataEntity ? (float) tableDataEntity.getZzl().doubleValue() : 0);

                columnData.add(xValue);
                splineData.add(zzl);
            }

            Series seriesCol = new Series();

            Map<String, String> tooltipCol = new HashMap<>();
            tooltipCol.put("valueSuffix", " K");

            seriesCol.setName(typename + "投运量");
            seriesCol.setType("column");
            seriesCol.setTooltip(tooltipCol);
            seriesCol.setyAxis(0);
            seriesCol.setData(columnData);
            seriesCol.setColor(!"".equals(color) ? color: Series.getColor(i));

            Series seriesSpl = new Series();

            Map<String, String> tooltipSpl = new HashMap<>();
            tooltipSpl.put("valueSuffix", " %");

            seriesSpl.setName(typename + "增长率");
            seriesSpl.setType("spline");
            seriesSpl.setTooltip(tooltipSpl);
            seriesSpl.setyAxis(1);
            seriesSpl.setData(splineData);
            seriesSpl.setColor(!"".equals(color) ? color: Series.getColor(i));

            series.add(seriesCol);
            series.add(seriesSpl);
        }

        m.put("categories", categories);
        m.put("series", series);

        j.setAttributes(m);

        return j;
    }

    @Override
    public AjaxJson cb(String queryType){
        AjaxJson j = new AjaxJson();
        Map<String, Object> m = new HashMap<>();

        List<Map<String, Object>> dateList = getDateList();
        List<Map<String, Object>> typeList = null;

        String color = "";
        if("gy".equals(queryType)){
            typeList = getTypeGyList();
            color = Series.getColor(1);

        }else if("my".equals(queryType)){
            typeList = getTypeMyList();
        }

        //横坐标
        List<String> categories = new ArrayList<>();
        for(Map<String, Object> date: dateList){
            categories.add(date.get("xDate").toString());
        }

        //纵坐标
        List<Series> series = new ArrayList<>();
        for(int i = 0; i < typeList.size(); i++){
            String typecode = (String) typeList.get(i).get("typecode");
            String typename = (String) typeList.get(i).get("typename");
            List<Double> columnData = new ArrayList<>();
            List<Float> splineData = new ArrayList<>();

            for(Map<String, Object> obj: dateList){
                Date date = (Date) obj.get("date");
                ZtTableDataEntity tableDataEntity = ztTableDataRepository.findByTypeAndDate(typecode, date);
                //本月抄表数
                double bycbs = null != tableDataEntity ? tableDataEntity.getBycbs() / 1000 : 0;
                //本月成功抄表数
                double bycgcbs = null != tableDataEntity ? tableDataEntity.getBycgcbs() / 1000 : 0;
                //抄表率
                float bycbl = null != tableDataEntity ? (float) (tableDataEntity.getBycbl() * 100) : 0;

                columnData.add(bycgcbs);
                splineData.add(bycbl);
            }

            Series seriesCol = new Series();

            Map<String, String> tooltipCol = new HashMap<>();
            tooltipCol.put("valueSuffix", " K");

            seriesCol.setName(typename + "成功数");
            seriesCol.setType("column");
            seriesCol.setTooltip(tooltipCol);
            seriesCol.setyAxis(0);
            seriesCol.setData(columnData);
            seriesCol.setColor(!"".equals(color) ? color: Series.getColor(i));

            Series seriesSpl = new Series();

            Map<String, String> tooltipSpl = new HashMap<>();
            tooltipSpl.put("valueSuffix", " %");

            seriesSpl.setName(typename + "抄表率");
            seriesSpl.setType("spline");
            seriesSpl.setTooltip(tooltipSpl);
            seriesSpl.setyAxis(1);
            seriesSpl.setData(splineData);
            seriesSpl.setColor(!"".equals(color) ? color: Series.getColor(i));

            series.add(seriesCol);
            series.add(seriesSpl);
        }

        m.put("categories", categories);
        m.put("series", series);

        j.setAttributes(m);

        return j;
    }

    @Override
    public AjaxJson gz(String queryType){
        AjaxJson j = new AjaxJson();
        Map<String, Object> m = new HashMap<>();

        List<Map<String, Object>> dateList = getDateList();
        List<Map<String, Object>> typeList = null;

        String color = "";
        if("gy".equals(queryType)){
            typeList = getTypeGyList();
            color = Series.getColor(2);

        }else if("my".equals(queryType)){
            typeList = getTypeMyList();
        }

        //横坐标
        List<String> categories = new ArrayList<>();
        for(Map<String, Object> date: dateList){
            categories.add(date.get("xDate").toString());
        }

        //纵坐标
        List<Series> series = new ArrayList<>();
        for(int i = 0; i < typeList.size(); i++){
            String typecode = (String) typeList.get(i).get("typecode");
            String typename = (String) typeList.get(i).get("typename");
            List<Float> columnData = new ArrayList<>();
            List<Float> splineData = new ArrayList<>();

            for(Map<String, Object> obj: dateList){
                Date date = (Date) obj.get("date");
                ZtTableDataEntity tableDataEntity = ztTableDataRepository.findByTypeAndDate(typecode, date);
                //本月新增
                float top = QjztUtil.getDivide(null != tableDataEntity ? tableDataEntity.getByxz() : 0, 1000);
                //维护量
                double bottom = null != tableDataEntity ? tableDataEntity.getWhl() / 1000 : 0;
                //故障率
                float percent = null != tableDataEntity ? (float) (tableDataEntity.getGzl() * 100) : 0;

                columnData.add(top);
                splineData.add(percent);
            }

            Series seriesCol = new Series();

            Map<String, String> tooltipCol = new HashMap<>();
            tooltipCol.put("valueSuffix", " K");

            seriesCol.setName(typename + "本月新增");
            seriesCol.setType("column");
            seriesCol.setTooltip(tooltipCol);
            seriesCol.setyAxis(0);
            seriesCol.setData(columnData);
            seriesCol.setColor(!"".equals(color) ? color: Series.getColor(i));

            Series seriesSpl = new Series();

            Map<String, String> tooltipSpl = new HashMap<>();
            tooltipSpl.put("valueSuffix", " %");

            seriesSpl.setName(typename + "故障率");
            seriesSpl.setType("spline");
            seriesSpl.setTooltip(tooltipSpl);
            seriesSpl.setyAxis(1);
            seriesSpl.setData(splineData);
            seriesSpl.setColor(!"".equals(color) ? color: Series.getColor(i));

            series.add(seriesCol);
            series.add(seriesSpl);
        }

        m.put("categories", categories);
        m.put("series", series);

        j.setAttributes(m);

        return j;
    }

    @Override
    public AjaxJson gzcz(String queryType){
        AjaxJson j = new AjaxJson();
        Map<String, Object> m = new HashMap<>();

        List<Map<String, Object>> dateList = getDateList();
        List<Map<String, Object>> typeList = null;

        String color = "";
        if("gy".equals(queryType)){
            typeList = getTypeGyList();
            color = Series.getColor(3);

        }else if("my".equals(queryType)){
            typeList = getTypeMyList();
        }

        //横坐标
        List<String> categories = new ArrayList<>();
        for(Map<String, Object> date: dateList){
            categories.add(date.get("xDate").toString());
        }

        //纵坐标
        List<Series> series = new ArrayList<>();
        for(int i = 0; i < typeList.size(); i++){
            String typecode = (String) typeList.get(i).get("typecode");
            String typename = (String) typeList.get(i).get("typename");
            List<Float> columnData = new ArrayList<>();
            List<Float> splineData = new ArrayList<>();

            for(Map<String, Object> obj: dateList){
                Date date = (Date) obj.get("date");
                ZtTableDataEntity tableDataEntity = ztTableDataRepository.findByTypeAndDate(typecode, date);
                //本月成功处置
                float top = QjztUtil.getDivide(null != tableDataEntity ? tableDataEntity.getBycgcz() : 0, 1000);
                //需要处置数
                double bottom = null != tableDataEntity ? tableDataEntity.getXyczl() / 1000 : 0;
                //本月修复率
                float percent = null != tableDataEntity ? (float) (tableDataEntity.getByxfl() * 100) : 0;

                columnData.add(top);
                splineData.add(percent);
            }

            Series seriesCol = new Series();

            Map<String, String> tooltipCol = new HashMap<>();
            tooltipCol.put("valueSuffix", " K");

            seriesCol.setName(typename + "已处置数");
            seriesCol.setType("column");
            seriesCol.setTooltip(tooltipCol);
            seriesCol.setyAxis(0);
            seriesCol.setData(columnData);
            seriesCol.setColor(!"".equals(color) ? color: Series.getColor(i));

            Series seriesSpl = new Series();

            Map<String, String> tooltipSpl = new HashMap<>();
            tooltipSpl.put("valueSuffix", " %");

            seriesSpl.setName(typename + "成功处置率");
            seriesSpl.setType("spline");
            seriesSpl.setTooltip(tooltipSpl);
            seriesSpl.setyAxis(1);
            seriesSpl.setData(splineData);
            seriesSpl.setColor(!"".equals(color) ? color: Series.getColor(i));

            series.add(seriesCol);
            series.add(seriesSpl);
        }

        m.put("categories", categories);
        m.put("series", series);

        j.setAttributes(m);

        return j;
    }

    /**
     * 得到X坐标日期
     * @return
     */
    private List<Map<String, Object>> getDateList(){
        String sql = "select DISTINCT d.date as date, DATE_FORMAT(d.date,'%Y%m') as xDate from zt_table_data d order by d.date DESC limit 0, 6";
        List<Map<String, Object>> list = primaryJdbc.queryForList(sql);

        Collections.reverse(list);
        return list;
    }

    /**
     * 得到表类型集合
     * @return
     */
    private List<Map<String, Object>> getTypeMyList(){
        String sql = "select tt.typecode, tt.typename from t_s_type tt left join t_s_typegroup gg on tt.typegroupid = gg.id where gg.typegroupcode = 'table_type' and tt.typecode != 'gyb'";
        List<Map<String, Object>> list = primaryJdbc.queryForList(sql);
        return list;
    }

    private List<Map<String, Object>> getTypeGyList(){
        String sql = "select tt.typecode, tt.typename from t_s_type tt left join t_s_typegroup gg on tt.typegroupid = gg.id where gg.typegroupcode = 'table_type' and tt.typecode = 'gyb'";
        List<Map<String, Object>> list = primaryJdbc.queryForList(sql);
        return list;
    }

}
